package com.xiaohe.gulimall.seckill.scheduled;

import com.xiaohe.gulimall.seckill.service.SeckillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * 秒杀商品定时上架
 * 每天晚上三点上线商品,当天00:00:00----23:59:59 明天00:00:00----23:59:59 后天00:00:00----23:59:59
 */
@Slf4j
@Service
public class SeckillSkuScheduled {

    @Autowired
    private SeckillService seckillService;

    @Autowired
    private RedissonClient redissonClient;

    private final String UPLOAD_LOCK="seckill:upload:lock";

    //TODO
    @Scheduled(cron = "* * 3 * * ?")
    public void uploadSexKillSkuLatest3Days(){
        log.info("上架的秒杀商品信息");
        RLock lock = redissonClient.getLock(UPLOAD_LOCK);
        lock.lock(10, TimeUnit.SECONDS);
        try {
            seckillService.uploadSexKillSkuLatest3Days();
        }finally {
            lock.unlock();
        }
    }
}
